/**
 * 订单信息(根据订单号查询)
 * 调用方法  地址栏后面加num(订单号)
 * 
 */

package ms.xml;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ms.model.Cart;
import ms.model.OrderItem;
import ms.service.CartManager;
import ms.service.OrderItemManager;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet implementation class SearchOrderBynumforxml
 */
public class SearchOrderBynumforxml extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchOrderBynumforxml() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String num = request.getParameter("num");
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		ServletContext context = request.getSession().getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(context);
		CartManager cartManager = (CartManager) ctx.getBean("cartManager");
		OrderItemManager orderItemManager = (OrderItemManager) ctx.getBean("orderItemManager");
		Cart cart = cartManager.loadByNum(num);
		List<OrderItem> orderitems = orderItemManager.getOrderItembyCart(cart);
		Document d = DocumentHelper.createDocument();
		// 创建根节点
		Element elements = d.addElement("orders");
		Element element = elements.addElement("order");
		element.addElement("num").addText(cart.getNum());
		element.addElement("date").addText(cart.getDate().toString());
		element.addElement("state").addText(cart.getState());
		element.addElement("allprice").addText(
				String.valueOf(cart.getAllprice()));
		for (OrderItem orderItem : orderitems) {
			element = elements.addElement("orderItem");
			element.addElement("item_name").addText(orderItem.getItem().getItem_name());
			element.addElement("number").addText(String.valueOf(orderItem.getNumber()));
			element.addElement("img_url").addText(orderItem.getItem().getImg_url());
			element.addElement("price").addText(
				String.valueOf(orderItem.getItem().getPrice()));
		}
		d.setXMLEncoding("utf-8");
		writer.print(d.asXML());
	}

}
